<%@ page language="java" import="java.util.*,ms.platform.system.model.*"
	pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
<!DOCTYPE html>
<html>
<head>
<base href="<%=basePath%>">
<title>分配权限</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<link rel="stylesheet" type="text/css" href="<%=basePath%>js/bootstrap-3.3.6/dist/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="<%=basePath%>css/bootstrapValidator.css" />
<script type="text/javascript" src="<%=basePath%>js/common/dialog.3.1.min.js"></script>
<link rel="stylesheet" type="text/css" href="<%=basePath%>css/mydialog.css" />
<link rel="stylesheet" type="text/css" href="<%=basePath%>css/style.css" />
</head>
<body>
	<div id="index-hav2">
		<div class="index-hav-right" id="index-hav-right-clear">
			<div class="row">
				<div class="col-md-9">
					<span id="icon-house"></span> <span id="arrows-left">首页</span> 
					<span id="arrows-left">></span><span>系统管理</span><span id="arrows-left">></span><span>角色管理</span>
					<!--  <button type="button" class="btn btn-primary" roleId id="allotRoleResource">提交更改</button>
					<span id="goback" class="goback" name="goback" style="margin-left: 4.5rem;cursor: pointer;">返回</span>-->
				</div>
				<div class="bank-box">
					<div class="lianlian platform">
						<div class="col-md-12" style="margin: 13px 0">
							<input name='roleId' type='hidden' value="${roleId}" id="roleId" >
							<div class="platform-title">
							
							<button type="button" class="btn btn-primary" id="goback" class="goback" name="goback" style="float:right;margin-right: .1rem;cursor: pointer;">返回</botton>
							   <button type="button" class="btn btn-primary" roleId id="allotRoleResource" style="float:right;margin-right: .1rem;">保存</button>
							</div>
							<div id="level" style="padding:10px;">
						    <span style="font-size:16px;font-weight:blod;"><input type="checkbox" style="margin-right:5px;" name="bigChecked" value=""><span id="allSelect">全选</span></span>
							<c:forEach items="${menuList}" var="menu">
							<ul class="level-1">
								&nbsp;&nbsp;&nbsp;&nbsp;
								<c:if test="${menu.isChecked == true}"><input name='firstCheckbox' class="firstCheckbox" type='checkbox' checked="checked" value="${menu.id}" pid="${menu.parentId}" id="${menu.id}"></c:if>
								<c:if test="${menu.isChecked == false}"><input name='firstCheckbox' class="firstCheckbox" type='checkbox' value="${menu.id}" pid="${menu.parentId}" id="${menu.id}" ></c:if>
								|-<span style="" class='label label-default'>${menu.name}</span></br>
								<c:if test="${menu.children.size()>0}">
								<c:forEach items="${menu.children}" var="m">
								<ul class="level-2">
									&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
									<c:if test="${m.isChecked == true}"><input name='secondCheckbox' class="secondCheckbox" type='checkbox' checked="checked" value="${m.id}" pid="${m.parentId}" id="${m.id}" ></c:if>
									<c:if test="${m.isChecked == false}"><input name='secondCheckbox' class="secondCheckbox" type='checkbox' value="${m.id}" pid="${m.parentId}" id="${m.id}" ></c:if>
									|-<span class='label label-default'>${m.name}</span></br>
										<ul class="level-3">
										<c:if test="${m.children.size()>0}">
										<c:forEach items="${m.children}" var="mm">
											&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
											<c:if test="${mm.isChecked == true}"><input name='thirdCheckbox' class="thirdCheckbox" checked="checked" type='checkbox' value="${mm.id}" pid="${mm.parentId}" id="${mm.id}"></c:if>
											<c:if test="${mm.isChecked == false}"><input name='thirdCheckbox' class="thirdCheckbox" type='checkbox' value="${mm.id}" pid="${mm.parentId}" id="${mm.id}"></c:if>
											|-<span class='label label-default'>${mm.name}</span></br>
										</c:forEach>
										</c:if>
										</ul>
										
								</ul>	
								</c:forEach>
								</c:if>
									
							</ul>
							</c:forEach>
							</div>
						</div>
						<div class="modal-footer">
          					<!-- <button type="button" class="btn btn-primary" roleId id="allotRoleResource">提交更改</button> -->
         				</div>
					</div>
				</div>
			</div>
		</div>
	</div>
	<div>
	</div>
</body>
<script src="<%=basePath %>js/common/jquery-2.1.0.js" type="text/javascript" charset="utf-8"></script>
<script src="<%=basePath %>js/rem.js" type="text/javascript" charset="utf-8"></script>
<script src="<%=basePath %>js/acount/recharge.js" type="text/javascript" charset="utf-8"></script>
<script src="<%=basePath %>js/acount/withdraw.js" type="text/javascript" charset="utf-8"></script>
<script src="<%=basePath %>js/acount/acount.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript" src="<%=basePath %>js/jquery-1.9.1.min.js" charset="utf-8"></script>
<script type="text/javascript" src="<%=basePath %>js/bootstrap-3.3.6/dist/js/bootstrap.min.js" charset="utf-8"></script>
<script type="text/javascript">
		$(document).ready(function(){
			
			
			//---------复选框部分----------------
			$(".level-2").hide();
			$(".level-3").hide();
			
			$("ul[class='level-1'] span").click(function(){
				$(this).parent().find("ul[class='level-2']").toggle();
			})
			$("ul[class='level-2']").children("span").click(function(){
				$(this).parent().find("ul[class='level-3']").toggle();
			})
			//全选按钮
			$("input[name='bigChecked']").click(function(){
				if($(this).prop("checked")){
					//勾选全选
					$("#level").find("input[type='checkbox']").prop("checked",true);
				}else if(!$(this).prop("checked")){
					//取消全选
					$("#level").find("input[type='checkbox']").prop("checked",false);
				}
			})
			//一级checkbox全选/全不选 
			$("input[name='firstCheckbox']").click(function() {		
				var one = $(this);
				if($(this).prop("checked")){
					//如果一级被选中，子集都被选中(二级三级都选中)
					$(this).parent().find("input[type='checkbox']").prop("checked",true);
					//如一级都被选中，全选被选中
					var inputs = $("#level").find("input[name='firstCheckbox']");
					//console.log(inputs)
					var bigArr = [];
					
					for(var i = 0;i < inputs.length;i ++){
						bigArr.push(inputs[i].checked);
					}
					//console.log(bigArr)
					function isTrue(x){
						return x == true;
					}
					if(bigArr.every(isTrue)){
						$("input[name='bigChecked']").prop("checked",true);
					} 
					
					
				}else if(!$(this).prop("checked")){
					//如果一级被取消，子集都取消(二级三级都取消)
					$(this).parent().find("input[type='checkbox']").prop("checked",false);
					//取消全选
					$("input[name='bigChecked']").prop("checked",false);
				}
			});
			//二级checkbox全选/全不选
			$("input[name='secondCheckbox']").click(function() {
				
				var two = $(this);
				if($(this).prop("checked")){
					two.parent().siblings("input[name='firstCheckbox']").prop("checked",true);//选中父级
					two.siblings().find("input[type='checkbox']").prop("checked",true);//设置当前元素及其子元素选中
				}
				if(!$(this).prop("checked")){
					//取消全选
					$("input[name='bigChecked']").prop("checked",false);
					//如果二级被取消，子集与父级被取消(一级三级被取消)
					$(this).parent().find("input[type='checkbox']").prop("checked",false);
					//$(this).parent().parent().find("input[name='firstCheckbox']").prop("checked",false);
					var bbb = $(this).parent().siblings().find("input[name='secondCheckbox']");
					//console.log(bbb)
					var arr = [];
					for(var i = 0;i < bbb.length;i ++){
						arr.push(bbb[i].checked);
					}
					//console.log(arr)
					function isFalse(x){
						return x == false;
					}
					if(arr.every(isFalse)){
						two.parent().siblings("input[name='firstCheckbox']").prop("checked",false);
					}
				}
			});
			
			//三级 checkbox全选/不全选
			$("input[name='thirdCheckbox']").click(function() {
				var three = $(this);
				//如取消三级判断后决定是否取消二级
				if(!$(this).prop("checked")){
					var ccc = $(this).siblings("input[name='thirdCheckbox']");
					var arr = [];
					for(var i = 0;i < ccc.length;i ++){
						arr.push(ccc[i].checked);
					}
					//console.log(arr)
					function isTrue(x){
						return x == true;
					}
					function isFalse(x){
						return x == false;
					}
					if(arr.every(isFalse)){
						three.parent().siblings("input[name='secondCheckbox']").prop("checked",false);
					}
					
					//判断三级取消后是否取消一级
					//判断二级无被选中项，一级则不选中
					var two1 = three.parent().parent().siblings().find("input[name='secondCheckbox']");
					var arr2 = [];
					for(var i = 0;i < two1.length;i ++){
						arr2.push(two1[i].checked);
					}
					
					if(arr2.every(isFalse)){
						three.parent().parent().siblings("input[name='firstCheckbox']").prop("checked",false);
					}
				}
				//如勾选三级，则选中二级
				if($(this).prop("checked")){
					three.parent().siblings("input[name='secondCheckbox']").prop("checked",true);//选中父级
					//如果二级被选中，一级也被选中
					//alert(three.parent().siblings("input[name='secondCheckbox']").prop("checked"))
					if(three.parent().siblings("input[name='secondCheckbox']").prop("checked")){
						three.parent().parent().siblings("input[name='firstCheckbox']").prop("checked",true);
					}
					
				}
				
				
				
				//如3级全选，则选中全选
				var inputs = $("#level").find("input[name='firstCheckbox']");
				//console.log(inputs)
				var bigArr = [];
				
				for(var i = 0;i < inputs.length;i ++){
					bigArr.push(inputs[i].checked);
				}
				//console.log(bigArr)
				function isTrue(x){
					return x == true;
				}
				if(bigArr.every(isTrue)){
					$("input[name='bigChecked']").prop("checked",true);
				}
				
			});
			
			//分配权限（给角色添加/取消权限）
			$("#allotRoleResource").click(function(){
				var str="";
				var type=0;
				$(".level-1").find("input[type='checkbox']").each(function() {
					if ($(this).prop("checked") == true	|| $(this).prop("checked") == "checked") {
						type=1;
					}else{
						type=0;
					}
					if (str == "") {
						str = $(this).val()+"-"+type;
					} else {
						str = str + ","	+ $(this).val()+"-"+type;
					}
				});
				  var roleId=$("input[name='roleId']").val();
				  $.ajax({            
	                  type:"post",
	                  url:"<%=path%>/system/resource/allotResource", 
	                  data:"roleId="+roleId+"&resourceIds="+str, 
	                  dataType:"json",
	                  success:function(data) {
	                	  showMessage(data.message);
	                	  $(".D_submit").click(function(){
	                		  $("button[data-dismiss='modal']").click();
		                	  window.location.href='<%=path%>/system/role/list';
	                	  })
	                	  
	                  }
				  }); 
			});
			
			//返回列表
			$("#goback").click(function(){
				//window.history.back();
				window.location.href="<%=path%>/system/role/list";
			})
			
		});
</script>
</html>
